package com.emontis.notiztapete.components;

import java.text.SimpleDateFormat;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;

import com.emontis.notiztapete.entities.Note;
import com.emontis.notiztapete.pages.Index;
import com.emontis.notiztapete.pages.notes.EditNotes;

public class StickyNote
{
  @Property
  @Parameter (required = true)
  private Note note;

  @Inject
  private Session session;

  @InjectPage
  private EditNotes editPage;

  Object onActionFromEdit (Note note)
  {
    this.editPage.setNote(note);
    return this.editPage;
  }

  @CommitAfter
  Object onActionFromDelete (Note note)
  {
    this.session.delete(note);
    return Index.class;
  }

  public String getCreationDateFormatted ()
  {
    return new SimpleDateFormat("'vom ' dd. MMMM yyyy").format(this.note.getCreation());
  }

}
